﻿/******************************************************************************/
#include "stdafx.h"
#include "Main.h"
/******************************************************************************/
STRUCT(ServerSelection , GuiObjs)
//{
   Bool wait_for_greet;

   static void Connect(ServerSelection &ss) {ss.connect(ss.getTextLine("address")());}

   STRUCT(TextLine , ::TextLine)
   //{
      virtual void update(C GuiPC &gpc)
      {
         super::update(gpc);
         if(ServerSelection *ss=(ServerSelection*)user)
         {
            if(Gui.kb()==this && Kb.bp(KB_ENTER)){Kb.eat(KB_ENTER); Connect(*ss);}
         }
      }
   };

   void create()
   {
      replaceTextLine<TextLine>(); load("gui/select server.gobj"); Gui+=T;
      getButton  ("connect").func(Connect, T);
      getTextLine("address").selectAll().activate().user=this;
   }

   void connect(SockAddr &server_addr)
   {
      if(!Server.clientConnectToServer(server_addr))Gui.msgBox("Error", "Can't connect to server at specified address");else
      {
         Server.tcpNoDelay(true);
         wait_for_greet=true;
      }
   }
   void connect(Str server_addr)
   {
      SockAddr sa;
      if(!sa.fromText(server_addr))Gui.msgBox("Error", "Invalid Server Address");else connect(sa);
   }
   
   ServerSelection()
   {
      wait_for_greet=false;
   }
}SelectServer;
/******************************************************************************/
Bool InitSelectServer()
{
   SelectServer.create();
   return true;
}
void ShutSelectServer()
{
   SelectServer.del();
}
/******************************************************************************/
Bool UpdateSelectServer()
{
   Gui.update();

   // check server state
   switch(Server.state())
   {
      case CONNECT_AWAIT_GREET     : if(Server.life()>5000){Gui.msgBox("Error", "Server Connection Timeout"); Server.del();} break;
      case CONNECT_VERSION_CONFLICT: Gui.msgBox("Error", "Version conflict between server and client.\nPlease update your software."); Server.del(); break;
      case CONNECT_GREETED         : if(SelectServer.wait_for_greet){SelectServer.wait_for_greet=false; ClientSendVersion(Server);} break;
   }

   // check if any data received from server
   if(Server.receive(0))switch(Server.data.getByte())
   {
      case CS_VERSION_CHECK:
      {
         if(ClientRecvVersion(Server.data))ClientSendInitConnection(Server);else Gui.msgBox("Error", "Server uses different game version");
      }break;

      case CS_INIT_CONN:
      {
         DateTime server_utc; if(ClientRecvInitConnection(Server, server_utc))StateLogin.set(0.2f);else Gui.msgBox("Error", "Error during connection initialization");
      }break;
   }

   return true;
}
void DrawSelectServer()
{
   D.clear(BLACK);
   Gui.draw();
}
/******************************************************************************/
State StateSelectServer(UpdateSelectServer, DrawSelectServer, InitSelectServer, ShutSelectServer);
/******************************************************************************/
